package cn.com.lyj6851.modules.mp.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.mp.domain.YdyWechatTemplate;
import cn.com.lyj6851.modules.mp.service.YdyWechatTemplateService;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatTemplateDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatTemplateQueryCriteria;
import cn.com.lyj6851.modules.mp.service.mapper.WechatTemplateMapper;
import cn.com.lyj6851.utils.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings({"all"})
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "ydyWechatTemplate")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YdyWechatTemplateServiceImpl extends BaseServiceImpl<WechatTemplateMapper, YdyWechatTemplate> implements YdyWechatTemplateService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YdyWechatTemplateQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdyWechatTemplate> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YdyWechatTemplateDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YdyWechatTemplate> queryAll(YdyWechatTemplateQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdyWechatTemplate.class, criteria));
    }


    @Override
    public void download(List<YdyWechatTemplateDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdyWechatTemplateDto ydyWechatTemplate : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("模板编号", ydyWechatTemplate.getTempkey());
            map.put("模板名", ydyWechatTemplate.getName());
            map.put("回复内容", ydyWechatTemplate.getContent());
            map.put("模板ID", ydyWechatTemplate.getTempid());
            map.put("添加时间", ydyWechatTemplate.getAddTime());
            map.put("状态", ydyWechatTemplate.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public YdyWechatTemplate findByTempkey(String recharge_success_key) {
        return this.getOne(new QueryWrapper<YdyWechatTemplate>().lambda()
                .eq(YdyWechatTemplate::getTempkey,recharge_success_key));
    }
}